/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.information.domain.Doc;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.information.service.DocService;
import me.zhengjie.modules.information.domain.vo.DocQueryCriteria;
import me.zhengjie.modules.information.mapper.DocMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

import static me.zhengjie.modules.biz.domain.enums.ErrorCode.CODE_ALREADY_EXIST;
import static me.zhengjie.modules.pay.enums.StatusEnum.STATUS_ON;

/**
 * @description 服务实现
 * @author yiwei.he
 * @date 2024-05-17
 **/
@Service
@RequiredArgsConstructor
public class DocServiceImpl extends ServiceImpl<DocMapper, Doc> implements DocService {

    private final DocMapper docMapper;

    @Override
    public PageResult<Doc> queryAll(DocQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(docMapper.findAll(criteria, page));
    }

    @Override
    public List<Doc> queryAll(DocQueryCriteria criteria){
        return docMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Doc resources) throws BizException {
        Doc doc = docMapper.selectOne(new QueryWrapper<Doc>().eq("doc_code", resources.getDocCode()));
        if(doc != null){
            throw new BizException(CODE_ALREADY_EXIST.getCode(),CODE_ALREADY_EXIST.getMessage());
        }
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Doc resources) {
        Doc doc = getById(resources.getId());
        doc.copy(resources);
        saveOrUpdate(doc);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Doc> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Doc doc : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("文档名", doc.getDocName());
            map.put("文档编码", doc.getDocCode());
            map.put("内容", doc.getDocContent());
            map.put("是否隐藏【0:不隐藏 1：隐藏】", doc.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Doc qryByCode(String docCode) {
        return docMapper.selectOne(new QueryWrapper<Doc>().eq("doc_code", docCode)
                .eq("is_show",STATUS_ON.getCode())
        );
    }
}